package com.whale;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.Html;
import android.text.Html.ImageGetter;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.whale.activity.WhaleBaseActivity;
import com.whale.util.FileUtil;

/**
 * 类PostStreamActivity.java的实现描述：分享美食
 * 
 * @author alibaba 2011-9-18 上午10:51:21
 */
public class PostStreamActivity extends WhaleBaseActivity {

    private static final String TAG                   = "PostStreamActivity";
    private static final int    REQUEST_IMAGE_CAPTURE = 1;
    private static final int    REQUEST_PHOTO_LIBRARY = 2;
    private static final int    REQUEST_CIRCLE_PEOPLE = 3;
    private static final int    REQUEST_IMAGE_VIEW    = 4;
    private File                mImageFile;
    private Uri                 mImageUri;
    private File                mFile;
    private static final int    MAX_BITMAP_SIZE       = 400;
    private Button              addCmameraBtn;
    private Button              addPhotoBtn;
    private Button              addAtBtn;
    private Button              addExpressionBtn;
    private Button              addLocationBtn;
    private EditText            contentEdit;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post_stream);
        TextView add_top_tip = (TextView) findViewById(R.id.add_top_tip);
        Intent intent = getIntent();
        String tip = intent.getStringExtra("tip");
        add_top_tip.setText(tip);

        addCmameraBtn = (Button) findViewById(R.id.add_cmamera_btn);
        addCmameraBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                openCapture();
            }
        });

        addPhotoBtn = (Button) findViewById(R.id.add_photo_btn);
        addPhotoBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                openPhotoLibrary();
            }
        });

        addAtBtn = (Button) findViewById(R.id.add_at_btn);

        addExpressionBtn = (Button) findViewById(R.id.add_expression_btn);

        addLocationBtn = (Button) findViewById(R.id.add_location_btn);

        contentEdit = (EditText) findViewById(R.id.stream_content);
    }

    /**
     * 打开本地相册
     */
    private void openPhotoLibrary() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        startActivityForResult(intent, REQUEST_PHOTO_LIBRARY);
    }

    /**
     * 打开照相机
     */
    private void openCapture() {
        try {
            mImageFile = FileUtil.getCameraFile(this);
            mImageUri = Uri.fromFile(mImageFile);
            if (null == mImageUri) {
                Log.e(TAG, "##########: openCapture mImageUri is null!");
            }
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri);
            startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }
    }

    private Bitmap getThumbBitmap(Uri uri) {
        return WhaleApplication.mImageCanvas.createThumbnailBitmap(uri, MAX_BITMAP_SIZE);
    }

    private ImageGetter getImageGetter() {
        return new ImageGetter() {

            public Drawable getDrawable(String source) {
                Drawable drawable = new BitmapDrawable(getThumbBitmap(Uri.fromFile(new File(source))));
                drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
                return drawable;
            }
        };
    }

    /**
     * 调用相机和相册后的回调
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            mImageFile = FileUtil.getCameraFile(this);
            mImageUri = Uri.fromFile(mImageFile);
            contentEdit.append("\n");
            contentEdit.append(Html.fromHtml("<img src='" + mImageFile.getPath() + "'/>", getImageGetter(), null));
            contentEdit.append("\n");
        } else if (requestCode == REQUEST_PHOTO_LIBRARY && resultCode == RESULT_OK) {
            mImageUri = data.getData();
            mImageFile = new File(FileUtil.getRealPathFromURI(mImageUri, this));
            Log.e(TAG, "##########: mImageUri:" + mImageFile.getPath());
            contentEdit.append("\n");
            contentEdit.append(Html.fromHtml("<img src='" + mImageFile.getPath() + "'/>", getImageGetter(), null));
            contentEdit.append("\n");
            //
            // contentEdit.append("\n");
            // contentEdit.append(Html.fromHtml("<img src='" + mImageFile.getPath() + "'/>", getImageGetter(), null));
            // contentEdit.append("\n");
            //
            // String text = contentEdit.getText().toString();
            // SpannableStringBuilder builder = new SpannableStringBuilder(text);
            // if (null == mImageUri) {
            // Log.e(TAG, "##########: mImageUri is null!");
            // }
            //
            // builder.setSpan(new ImageSpan(getThumbBitmap(mImageUri)), text.length(), text.length(),
            // Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            // contentEdit.setText(builder);

        } else if (requestCode == REQUEST_CIRCLE_PEOPLE) {
            // 选择的分组和好友
            // getTextViewList(choosedCirclesList, choosedMemberList);
        }
    }

}
